<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Item_add extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->verify_user();
	}

	public function index()
	{
		$id = intval($this->uri->segment(3));

		if ($id) {
			$d = $this->d->find(array('table' => 'items', 'where' => 'id=' . $id));

			if ($d['step'] == 2) {
				$d['coins'] = $this->d->rows(array('table' => 'coin', 'where' => 'status=1 and is_db=0'));
			}
			else if ($d['step'] == 3) {
				$d['coin'] = $this->d->find(array('table' => 'coin', 'where' => 'id=' . $d['coin_id']));
				$d['db_coin'] = $this->d->find(array('table' => 'coin', 'where' => 'id=' . $d['db_id']));
			}
		}
		else {
			$d = array();
		}

		$d['db_coins'] = $this->db->where('is_db', 1)->get('coin')->result_array();
		$this->show(array('header', 'user_left', 'item_add', 'footer'), $d);
	}

	public function save1()
	{
		$id = intval($this->input->post('id'));
		$name = $this->input->post('name');
		$intro = $this->input->post('intro');
		$day = $this->input->post('day');
		$begin = $this->input->post('begin');
		$end = $this->input->post('end');
		$logo = $this->input->post('logo');
		$img = $this->input->post('img');

		if (!$name) {
			ajax_return(1, '请输入项目名称');
		}

		if (!$intro) {
			ajax_return(1, '请输入项目简介');
		}

		if (!$day) {
			ajax_return(1, '请输入项目天数');
		}

		if (!$begin) {
			ajax_return(1, '请输入开始时间');
		}

		if (!$end) {
			ajax_return(1, '请输入结束时间');
		}

		$data['name'] = $name;
		$data['intro'] = $intro;
		$data['day'] = $day;
		$data['begin'] = strtotime($begin);
		$data['end'] = strtotime($end);
		$data['logo'] = $logo;
		$data['img'] = $img;
		$data['step'] = 1;
		$data['user_id'] = $this->user['id'];
		$data['add_time'] = time();

		if (0 < $id) {
			$rs = $this->db->where('id', $id)->update('items', $data);
		}
		else {
			$rs = $this->db->insert('items', $data);
		}

		if ($rs) {
			ajax_return(0, '保存成功', site_url('item_add/index/' . $this->db->insert_id()));
		}
		else {
			ajax_return(1, '保存失败');
		}
	}

	public function save2()
	{
		$id = intval($this->input->post('id'));
		$content = $this->input->post('content');

		if (!$id) {
			ajax_return(1, '找不到项目');
		}

		if (!$content) {
			ajax_return(1, '请输入详细介绍');
		}

		$data['content'] = $content;
		$data['step'] = 2;
		$rs = $this->db->where('id', $id)->update('items', $data);

		if ($rs) {
			ajax_return(0, '保存成功', site_url('item_add/index/' . $id));
		}
		else {
			ajax_return(1, '保存失败');
		}
	}

	public function save3()
	{
		$id = intval($this->input->post('id'));
		$target_amount = num($this->input->post('target_amount'));
		$db_total = num($this->input->post('db_total'));
		$db_id = intval($this->input->post('db_id'));
		$db_rate = num($this->input->post('db_rate'));
		$coin_id = intval($this->input->post('coin_id'));
		$repay_day = intval($this->input->post('repay_day'));
		$book = $this->input->post('book');
		$is_over = intval($this->input->post('is_over'));
		$is_lock = intval($this->input->post('is_lock'));
		$is_lock_support = intval($this->input->post('is_lock_support'));

		if (!$id) {
			ajax_return(1, '找不到项目');
		}

		if (!$target_amount) {
			ajax_return(1, '请输入目标金额');
		}

		$data['target_amount'] = $target_amount;
		$data['coin_id'] = $coin_id;
		$data['repay_day'] = $repay_day;
		$data['book'] = $book;
		$data['step'] = 3;
		$data['db_total'] = $db_total;
		$data['db_id'] = $db_id;
		$data['db_rate'] = $db_rate;
		$data['is_over'] = $is_over;
		$data['is_lock'] = $is_lock;
		$data['is_lock_support'] = $is_lock_support;
		$rs = $this->db->where('id', $id)->update('items', $data);

		if ($rs) {
			ajax_return(0, '保存成功', site_url('item_add/index/' . $id));
		}
		else {
			ajax_return(1, '保存失败');
		}
	}

	public function submit()
	{
		$id = intval($this->input->post('id'));

		if (!$id) {
			ajax_return(1, '找不到项目');
		}

		$data['step'] = 4;
		$data['status'] = 1;
		$rs = $this->db->where('id', $id)->update('items', $data);

		if ($rs) {
			ajax_return(0, '提交成功', site_url('item_add/index/' . $id));
		}
		else {
			ajax_return(1, '提交失败');
		}
	}

	public function upload()
	{
		$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'] . '/upload/item/';
		$config['allowed_types'] = 'gif|jpg|png|pdf';
		$config['max_size'] = 1500;
		$config['max_width'] = 2000;
		$config['max_height'] = 1000;
		$config['file_name'] = rand(1000, 9999) . time();
		$config['file_ext_tolower'] = true;
		$this->load->library('upload', $config);

		if (!$this->upload->do_upload('Filedata')) {
			$data['error'] = 1;
			$data['msg'] = $_FILES['Filedata']['type'];
		}
		else {
			$data['error'] = 0;
			$data['msg'] = '上传成功';
			$d = $this->upload->data();
			$data['file_name'] = $d['file_name'];
		}

		echo json_encode($data);
	}
}

?>
